package com.yan;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Random;
import java.util.concurrent.TimeoutException;


/*
    消息生产者
 */
public class Producer {

    private static Random random = new Random();

    public static void main(String[] args) throws Exception, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://root:root@1.116.195.238:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //申明direct类型的交换器 交换器和消息列表的绑定不需要在这里处理
        channel.exchangeDeclare(QueueConstants.exchangeName, BuiltinExchangeType.TOPIC, true, false, null);


        for (int i = 0; i < 100; i++) {
            String area = QueueConstants.LogArea.forIndex(random.nextInt(100) % QueueConstants.LogArea.values().length);
            String service = QueueConstants.LogService.forIndex(random.nextInt(100) % QueueConstants.LogService.values().length);
            String level = QueueConstants.LogLevel.forIndex(random.nextInt(100) % QueueConstants.LogLevel.values().length);
            String routingKey = area + "." + service + "." + level;
            String message = "LOG    这是:[" + routingKey + "]  服务器发来的消息   index=" + i;

            channel.basicPublish(QueueConstants.exchangeName, routingKey, null, (message).getBytes());
        }

        channel.close();
        connection.close();

    }

}
